/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.ordersLine.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import qs.crm.annotation.Log;
import qs.crm.modules.client.ordersLine.domain.OrdersLine;
import qs.crm.modules.client.ordersLine.service.OrdersLineService;
import qs.crm.modules.client.ordersLine.service.dto.OrdersLineQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://eladmin.vip
* @author tong
* @date 2023-06-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "订单详情管理")
@RequestMapping("/api/ordersLine")
public class OrdersLineController {

    private final OrdersLineService ordersLineService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ordersLine:list')")
    public void exportOrdersLine(HttpServletResponse response, OrdersLineQueryCriteria criteria) throws IOException {
        ordersLineService.download(ordersLineService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询订单详情")
    @ApiOperation("查询订单详情")
    @PreAuthorize("@el.check('ordersLine:list')")
    public ResponseEntity<Object> queryOrdersLine(OrdersLineQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ordersLineService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增订单详情")
    @ApiOperation("新增订单详情")
    @PreAuthorize("@el.check('ordersLine:add')")
    public ResponseEntity<Object> createOrdersLine(@Validated @RequestBody OrdersLine resources){
        return new ResponseEntity<>(ordersLineService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改订单详情")
    @ApiOperation("修改订单详情")
    @PreAuthorize("@el.check('ordersLine:edit')")
    public ResponseEntity<Object> updateOrdersLine(@Validated @RequestBody OrdersLine resources){
        ordersLineService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除订单详情")
    @ApiOperation("删除订单详情")
    @PreAuthorize("@el.check('ordersLine:del')")
    public ResponseEntity<Object> deleteOrdersLine(@RequestBody Long[] ids) {
        ordersLineService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}